package com.qf.service;

import com.qf.dao.UserMapper;
import com.qf.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

//采用默认的id命名规则   类名的首字母小写。
//等价于<bean id="userServiceImpl" class="com.qf.service.UserServiceImpl">

@Service("userService")   //等于bean创建了对象 命名默认在类名后加impl （定义名字）
@Scope("prototype") //当前类 产生多列
@Transactional(rollbackFor = Exception.class)//当前类所有方法都有事务

public class UserServiceImpl implements UserService {
    @Autowired// 自动到工厂 里找 并与之匹配
//   @Resource//基于名称  在工厂里找userMapper
    @Qualifier("userMapper")
    private UserMapper userMapper;

    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public User findById(Integer id) {
        User user = userMapper.findUserById(id);
        return user;
    }


    public int updateUser(User user) {
        int i = userMapper.updateUser(user);
//        int o=10/0;
        return i;
    }

    public int insertUser(User user) {
        int i = userMapper.insertUser(user);
        return i;
    }

    public int deleteById(int id) {
        int delete = userMapper.delete(id);
        return delete;
    }
}
